package com.gitee.feizns.quickstart.quickio.example;

import com.github.artbits.quickio.api.Collection;
import com.github.artbits.quickio.api.JDB;
import com.github.artbits.quickio.core.IOEntity;
import com.github.artbits.quickio.core.QuickIO;

import java.util.List;
import java.util.function.Consumer;

/**
 * 实体示例
 * @author feizns
 * @since 2025/2/13
 */
public class EntityExample {

    public static class Book extends IOEntity {
        public String name;
        public String author;
        public Double price;
        public static Book of(Consumer<Book> consumer) {
            Book book = new Book();
            consumer.accept(book);
            return book;
        }
    }


    public static void main(String[] args) {
        try( JDB db = QuickIO.db("example_db") ) {

            Collection<Book> collection = db.collection(Book.class);

            collection.save(Book.of(b -> {
                b.name = "On java 8";
                b.author = "Bruce Eckel";
                b.price = 129.8;
            }));

            List<Book> books = collection.findAll();
            books.forEach(IOEntity::printJson);
        }
    }

}
